<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Services\DingTalkService;
use Illuminate\Http\Request;

class DingTalkController extends Controller
{
    protected $dingTalkService;

    public function __construct(DingTalkService $dingTalkService)
    {
        $this->dingTalkService = $dingTalkService;
    }

    /**
     * 获取企业CorpId (前端需要)
     */
    public function getCorpId()
    {
        return response()->json([
            'success' => true,
            'data' => [
                'corpId' => $this->dingTalkService->getCorpId()
            ]
        ]);
    }

    /**
     * 根据authCode获取用户信息
     */
    public function getUserInfo(Request $request)
    {
        $request->validate([
            'authCode' => 'required|string'
        ]);

        try {
            $userInfo = $this->dingTalkService->getUserInfoByCode($request->authCode);

            return response()->json([
                'success' => true,
                'data' => $userInfo
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => $e->getMessage()
            ], 500);
        }
    }
}